<!--评论模块-->
<template>

  <div class="container">
    <div class="comment" v-for="item in comments">
      <div class="info">
        <img class="avatar" :src="item.FromAvatar" width="36" height="36"/>
        <div class="right">
          <div class="name">{{item.FromName}}</div>
          <div class="date">{{item.Date}}</div>
        </div>
      </div>
      <div class="content">{{item.Content}}</div>
      <div class="control">
        <span class="like" :class="{active: item.isLike}" @click="likeClick(item)">
          <i class="el-icon-like"></i>
          <span class="el-icon-lollipop">{{item.LikeNum > 0 ? item.LikeNum + '人赞' : '赞'}}</span>
        </span>
        <span class="comment-reply" @click="showCommentInput(item)">
          <i class="el-icon-s-promotion"></i>
          <span>回复</span>
        </span>
      </div>
      <div class="reply">
        <div class="item" v-for="reply in item.Reply">
          <div class="reply-content">
            <span class="from-name">{{reply.FromName}}</span><span>: </span>
            <span class="to-name">@{{reply.ToName}}</span>
            <span>{{reply.Content}}</span>
          </div>
          <div class="reply-bottom">
            <span>{{reply.Date}}</span>
            <span class="reply-text" @click="showCommentInput(item, reply)">
              <i class="el-icon-s-promotion"></i>
              <span>回复</span>
            </span>
          </div>
        </div>
        <div class="write-reply" v-if="item.Reply.length > 0" @click="showCommentInput(item)">
          <i class="el-icon-edit"></i>
          <span class="add-comment">添加新评论</span>
        </div>
        <transition name="fade">
          <div class="input-wrapper" v-if="showItemId === item.Id">
            <el-input class="gray-bg-input"
                      v-model="inputComment"
                      type="textarea"
                      :rows="3"
                      autofocus
                      placeholder="写下你的评论">
            </el-input>
            <div class="btn-control">
              <span class="cancel" @click="cancel">取消</span>
              <el-button class="btn" type="success" round @click="commitComment(item)">确定</el-button>
            </div>
          </div>
        </transition>
      </div>
    </div>
  </div>
</template>

<script>

  import Vue from 'vue'

  export default {
    props: {
      comments: {
        type: Array,
        required: true
      }
    },
    components: {},
    data() {
      return {
        inputComment: '',
        showItemId: '',
        myReply:'',
      }
    },
    computed: {},
    methods: {
      /**
       * 点赞
       */
      likeClick(item) {
        if (item.isLike === null) {
          Vue.$set(item, "isLike", true);
          item.LikeNum++
        } else {
          console.log(item.LikeNum)
          if (item.isLike) {
            item.LikeNum--
          } else {
            item.LikeNum++
          }
          item.isLike = !item.isLike;
        }
      },

      /**
       * 点击取消按钮
       */
      cancel() {
        this.showItemId = ''
      },
      init() {
        let that = this;
        let formData = new FormData();
        formData.append("uid","0");
        that.axios({
          url:'/api/commentGo',
          method:'post',
          data:formData,
        }).then(function (response) {
          if(response.status === 200){
            console.log("芒果发送的数据",response.data)
            that.comments.Reply = response.data

            console.log(that.comments)
          }

        })
      },
      /**
       * 提交评论
       * reply
       Id         int    // 楼层id
       CommentId  int    // myComment 的属性 ownerId
       FromId     int    //评论者id
       FromName   string //评论者昵称
       FromAvatar string //评论者头像
       ToId       int    //被评论者id
       ToName     string //被评论者昵称
       ToAvatar   string //被评论者头像
       Content    string //评论内容
       Date       string //评论时间  18736082862
       *item
       Id         int    //楼层id
       Date       string //时间
       OwnerId    int    //文章id  uid
       FromId     int    //评论者id
       FromName   string //评论者昵称
       FromAvatar string //评论者头像
       LikeNum    int    //点赞人数
       Content    string //评论内容
       Reply      Reply  //子评论
       */
      commitComment(item) {
        let that = this
        if (this.inputComment[0] == "@"){
          console.log("我要喷人！！！",that.myReply.FromName);

          let formData = new FormData();
          let myDate = new Date();
          formData.append("ToName", that.myReply.FromName);
          formData.append("ToId", that.myReply.FromId);
          formData.append("ToAvatar", that.myReply.FromAvatar);
          formData.append("CommentId", that.myReply.CommentId);
          formData.append("Id",that.myReply.Id);//文章id  uid
          formData.append("FromId", "1");
          formData.append("FromName", "麦克雷");
          formData.append("FromAvatar", "https://s2.ax1x.com/2019/05/08/E63ulQ.jpg");
          formData.append("Content", this.inputComment);
          formData.append("Date", myDate);
          //console.log(item.FromName,item.OwnerId,item.Id)
          //console.log(that.myReply.FromName,that.myReply.CommentId,that.myReply.Id)
          that.axios({
            url: '/api/replyGo',
            method: 'post',
            data: formData,
          }).then(function (response) {
            if (response.status === 200) {
              console.log("芒果发送的数据", response.data)
              that.$router.go(0)
            }
          })

        }else {
          console.log("commitComment", this.inputComment);
          //console.log("层主所有信息", item)
          let that = this;
          let formData = new FormData();
          let myDate = new Date();
          formData.append("ToName", item.FromName);
          formData.append("ToId", item.FromId);
          formData.append("ToAvatar", item.FromAvatar);
          formData.append("CommentId", item.OwnerId);
          formData.append("Id", item.Id);//文章id  uid
          formData.append("FromId", "666666");
          formData.append("FromName", "m12");
          formData.append("FromAvatar", "https://s2.ax1x.com/2019/05/08/E63ulQ.jpg");
          formData.append("Content", this.inputComment);
          formData.append("Date", myDate);
          that.axios({
            url: '/api/replyGo',
            method: 'post',
            data: formData,
          }).then(function (response) {
            if (response.status === 200) {
              console.log("芒果发送的数据", response.data)
              that.$router.go(0)
            }
          })
        }
      },

      /**
       * 点击评论按钮显示输入框
       * item: 当前大评论
       * reply: 当前回复的评论
       * 	Id         int    //楼层id
       Date       string //时间
       OwnerId    int    //文章id  uid
       FromId     int    //评论者id
       FromName   string //评论者昵称
       FromAvatar string //评论者头像
       LikeNum    int    //点赞人数
       Content    string //评论内容
       Reply      Reply  //子评论
       */
      showCommentInput(item, reply) {
        let that = this
        if (reply) {
          this.inputComment = "@" + reply.FromName + ":";
          that.myReply = reply;
        } else {
          this.inputComment = ''
        }
        this.showItemId = item.Id

      }

    },
    created() {
      console.log("created Func",this.comments)
    }
  }
</script>

<style scoped >

  .container {
    padding: 0 10px;
    box-sizing: border-box;
  }
  .container .comment {
    display: flex;
    flex-direction: column;
    padding: 10px;
    border-bottom: 1px solid #F2F6FC;
  }
  .container .comment .info {
    display: flex;
    align-items: center;
  }
  .container .comment .info .avatar {
    border-radius: 50%;
  }
  .container .comment .info .right {
    display: flex;
    flex-direction: column;
    margin-left: 10px;
  }
  .container .comment .info .right .name {
    font-size: 16px;
    color: #303133;
    margin-bottom: 5px;
    font-weight: 500;
  }
  .container .comment .info .right .date {
    font-size: 12px;
    color: #909399;
  }
  .container .comment .content {
    font-size: 16px;
    color: #303133;
    line-height: 20px;
    padding: 10px 0;
  }
  .container .comment .control {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #909399;
  }
  .container .comment .control .like {
    display: flex;
    align-items: center;
    margin-right: 20px;
    cursor: pointer;
  }
  .container .comment .control .like.active, .container .comment .control .like:hover {
    color: #409EFF;
  }
  .container .comment .control .like .iconfont {
    font-size: 14px;
    margin-right: 5px;
  }
  .container .comment .control .comment-reply {
    display: flex;
    align-items: center;
    cursor: pointer;
  }
  .container .comment .control .comment-reply:hover {
    color: #333;
  }
  .container .comment .control .comment-reply .iconfont {
    font-size: 16px;
    margin-right: 5px;
  }
  .container .comment .reply {
    margin: 10px 0;
    border-left: 2px solid #DCDFE6;
  }
  .container .comment .reply .item {
    margin: 0 10px;
    padding: 10px 0;
    border-bottom: 1px dashed #EBEEF5;
  }
  .container .comment .reply .item .reply-content {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #303133;
  }
  .container .comment .reply .item .reply-content .from-name {
    color: #409EFF;
  }
  .container .comment .reply .item .reply-content .to-name {
    color: #409EFF;
    margin-left: 5px;
    margin-right: 5px;
  }
  .container .comment .reply .item .reply-bottom {
    display: flex;
    align-items: center;
    margin-top: 6px;
    font-size: 12px;
    color: #909399;
  }
  .container .comment .reply .item .reply-bottom .reply-text {
    display: flex;
    align-items: center;
    margin-left: 10px;
    cursor: pointer;
  }
  .container .comment .reply .item .reply-bottom .reply-text:hover {
    color: #333;
  }
  .container .comment .reply .item .reply-bottom .reply-text .icon-comment {
    margin-right: 5px;
  }
  .container .comment .reply .write-reply {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #909399;
    padding: 10px;
    cursor: pointer;
  }
  .container .comment .reply .write-reply:hover {
    color: #303133;
  }
  .container .comment .reply .write-reply .el-icon-edit {
    margin-right: 5px;
  }
  .container .comment .reply .fade-enter-active, .container .comment .reply fade-leave-active {
    transition: opacity 0.5s;
  }
  .container .comment .reply .fade-enter, .container .comment .reply .fade-leave-to {
    opacity: 0;
  }
  .container .comment .reply .input-wrapper {
    padding: 10px;
  }
  .container .comment .reply .input-wrapper .gray-bg-input, .container .comment .reply .input-wrapper .el-input__inner {
    /*background-color: #67C23A;*/
  }
  .container .comment .reply .input-wrapper .btn-control {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding-top: 10px;
  }
  .container .comment .reply .input-wrapper .btn-control .cancel {
    font-size: 16px;
    color: #606266;
    margin-right: 20px;
    cursor: pointer;
  }
  .container .comment .reply .input-wrapper .btn-control .cancel:hover {
    color: #333;
  }
  .container .comment .reply .input-wrapper .btn-control .confirm {
    font-size: 16px;
  }
</style>
